<html>
<head>
<meta charset="UTF-8">
<title>退菜处理</title>
<link id="easyuiTheme" rel="stylesheet" type="text/css"
	href="jslib/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css"
	href="jslib/easyui/themes/icon.css" />
<script type="text/javascript" src="jslib/easyui/jquery.min.js"></script>
<script type="text/javascript" src="jslib/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="jslib/easyui/locale/easyui-lang-zh_CN.js" charset="utf-8"></script>
</head>
<body>
	<table id="tt" class="easyui-datagrid"
		style="width: auto; height: 400px" url="saleOrderLoadItemAction"
		toolbar="#tb" title="退菜" singleSelect="true" iconCls="icon-save"
		rownumbers="true" pagination="true">
		<thead>
			<tr>
				<th field="itemId" width="180">订单编号</th>
				<th field="productId" width="180">菜品ID</th>
				<th field="code" width="180">菜品编码</th>
				<th field="name" width="180">菜品名称</th>
				<th field="description" width="180">菜品描述</th>
				<th field="price" width="80">单价</th>
				<th field="quanity" width="80">数量</th>
				<th field="total" width="80">总价格</th>
			</tr>
		</thead>
	</table>
	<div id="tb" style="padding: 3px">
		<span>订单号:</span> <input id="saleOrderNO" value="XF-1430995094862-370"
			style="line-height: 26px; border: 1px solid #ccc"><a href="#"
			class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="doSearch()">查找</a> <span>退菜单号:</span>
		<input id="saleReturnNO" value="" readOnly="true"
			style="line-height: 26px; width: 200px; border: 1px solid #ccc">
		<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true"
			onclick="saleReturn()">退菜</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true"
			onclick="clean()">reset</a>
	</div>

	<!-- 弹出框修改 -->
	<div id="dialog" class="easyui-dialog"
		style="width: 400px; height: 280px; padding: 10px 20px" closed="true"
		buttons="#dlg-buttons">
		<form id="form" method="post">
			<input type="hidden" id="saleNO" name="saleNO"
				data-options="required:true" /> <input name="itemId" type="hidden"
				data-options="required:true" /> <input name="returnNO"
				type="hidden" id="returnNO" data-options="required:true" /><input
				name="productId" type="hidden" data-options="required:true" />
			<div>
				<label>菜品编码</label> <input name="code" readOnly="true"
					data-options="required:true" />
			</div>
			<div>
				<label>菜品名称</label> <input name="name" readOnly="true"
					data-options="required:true" />
			</div>
			<div>
				<label>单价</label> <input name="price" readOnly="true"
					data-options="required:false" />
			</div>
			<div>
				<label>数量</label> <input name="quanity" readOnly="true"
					data-options="required:false" />
			</div>
			<div>
				<label>总价格</label> <input name="total" readOnly="true"
					data-options="required:false" />
			</div>
			<div>
				<label>退菜原因</label> <input name="reason" 
					data-options="required:false" />
			</div>
		</form>
	</div>
	<!-- END -->
	<script type="text/javascript">
		/* 显示Dialog*/
		function openDialog(title) {
			var saleOrderNO = document.getElementById('saleOrderNO').value;
			document.getElementById('saleNO').value = saleOrderNO;
			var saleReturnNO = document.getElementById('saleReturnNO').value;
			document.getElementById('returnNO').value = saleReturnNO;
			$("#dialog").dialog({
				resizable : false,
				modal : true,
				buttons : [ { //设置下方按钮数组
					text : '退菜',
					iconCls : 'icon-save',
					handler : function() {
						save();
					}
				}, {
					text : '取消',
					iconCls : 'icon-cancel',
					handler : function() {
						closeDialog();
					}
				} ]
			});
			$("#dialog").dialog('setTitle', title);
			$("#dialog").dialog('open');
		}
		/* 关闭Dialog*/
		function closeDialog() {
			$("#form").form('clear'); // 清空form的数据
			$("#dialog").dialog('close');// 关闭dialog
		}
		/* 退菜窗口*/
		function saleReturn() {
			var row = $('#tt').datagrid('getSelected'); //// 得到选中的一行数据
			//如果没有选中记录
			if (row == null) {
				$.messager.alert("提示", "请选择一条记录", 'info');
				return;
			}
			openDialog('处理退菜'); // 显示更新数据dialog窗口
			$("#form").form('load', row); // 加载选择行数据
			url = 'saleReturnAction'; //后台更新数据action
		}
		function doSearch() {
			var saleo = document.getElementById('saleOrderNO').value;
			if(saleo!=""){
			$('#tt').datagrid('load', {
				saleOrderNO : $('#saleOrderNO').val()
			});
			//如果没有创建新的退菜单号
			var saleReturnNO = document.getElementById('saleReturnNO').value
			if (saleReturnNO == "") {
				createNewReturn();
			}}else{
				alert("未输入单号");
			}
		}
		//创建新的退菜单号
		function createNewReturn() {
			//提交到后台的action
			$.post('saleReturnCreateAction', {}, function(result) {
				document.getElementById('saleReturnNO').value = result.msg;
			}, 'json');
		}
		function clean(){
			document.getElementById('saleReturnNO').value ="";
			document.getElementById('saleOrderNO').value="";
		}
		/* 保存数据*/
		function save() {
			$('#form').form('submit', {
				url : url, //提交地址
				onSubmit : function() {
					return $(this).form('validate'); //前台字段格式校验
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (result.success) {
						closeDialog();// 调用closeDialog;    
						doSearch();// 重新加载
						$.messager.show({ //显示正确信息
							title : '提示',
							msg : result.msg
						});
					} else {
						$.messager.show({ //显示错误信息
							title : '错误',
							msg : result.msg
						});
					}
				}
			});
		}
	</script>
</body>
</html>